package com.hoshiibuy.goodsAllocation.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiibuy.common.aop.Logable;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.goodsAllocation.dao.GoodsSubWarehouseMapper;
import com.hoshiibuy.goodsAllocation.entity.GoodsSubWarehouse;
import com.hoshiibuy.goodsAllocation.feign.IdGeneratorService;
import com.hoshiibuy.goodsAllocation.service.IGoodsSubWarehouseService;

/**
 * <p>
 * 商品分仓 服务实现类
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-28
 */
@Service
public class GoodsSubWarehouseServiceImpl extends ServiceImpl<GoodsSubWarehouseMapper, GoodsSubWarehouse>
		implements IGoodsSubWarehouseService {

	@Autowired
	private GoodsSubWarehouseMapper goodsSubWarehouseMapper;

	@Autowired
	private IdGeneratorService idGeneratorService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "GoodsSubWarehouse" }, allEntries = true)
	@Logable(authName = "商品分配-商品分仓信息管理-新增商品分仓信息")
	public Response insert(GoodsSubWarehouse param) {
		String id = idGeneratorService.queryGeneratorId();
		if (StringUtils.isBlank(id)) {
			throw new ServiceException(CommonError.error("id服务失败"));
		}
		// TODO 检查总仓ID是否有效
		// TODO 检查SKUID是否有效
		// TODO 检查分仓总库存是否≤总仓库存
		param.setId(Long.parseLong(id));
		param.setCreatedtime(LocalDateTime.now());
		int count = goodsSubWarehouseMapper.insert(param);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("新增失败"));
		}
		return Response.success("新增成功");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "GoodsSubWarehouse" }, allEntries = true)
	@Logable(authName = "商品分配-商品分仓信息管理-修改商品分仓信息")
	public Response updateDataById(GoodsSubWarehouse param) {
		// TODO 检查总仓ID是否有效
		// TODO 检查SKUID是否有效
		// TODO 检查分仓总库存是否≤总仓库存
		param.setUpdatetime(LocalDateTime.now());
		int count = goodsSubWarehouseMapper.updateById(param);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("修改失败"));
		}
		return Response.success("修改成功");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "GoodsSubWarehouse" }, allEntries = true)
	@Logable(authName = "商品分配-商品分仓信息管理-删除商品分仓信息")
	public Response deleteDataById(String id) {
		int count = goodsSubWarehouseMapper.deleteById(id);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("删除失败"));
		} else {
			return Response.success("删除成功");
		}
	}

	@Override
	@Cacheable(cacheNames = "GoodsSubWarehouse", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
	@Logable(authName = "商品分配-商品分仓信息管理-查询商品分仓信息详情")
	public Response getDetail(String id) {
		GoodsSubWarehouse data = goodsSubWarehouseMapper.selectById(id);
		if (data == null) {
			return Response.failed("数据不存在");
		} else {
			return Response.data(data);
		}
	}

	@Override
	@Cacheable(cacheNames = "GoodsSubWarehouse", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5(Wrapper(SqlSelect=' +#queryWrapper.getSqlSelect() + ', SqlSegment=' +#queryWrapper.getSqlSegment() + ')')")
	@Logable(authName = "商品分配-商品分仓信息管理-查询商品分仓信息列表")
	public Response<List<GoodsSubWarehouse>> getList(QueryWrapper<GoodsSubWarehouse> queryWrapper) {
		return Response.data(goodsSubWarehouseMapper.selectList(queryWrapper));
	}

	@Override
	@Cacheable(cacheNames = "GoodsSubWarehouse", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' +#page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' +#queryWrapper.getSqlSelect() + ', SqlSegment=' +#queryWrapper.getSqlSegment() + ')')")
	@Logable(authName = "商品分配-商品分仓信息管理-分页查询商品分仓信息")
	public Response getPage(IPage<GoodsSubWarehouse> page, QueryWrapper<GoodsSubWarehouse> queryWrapper) {
		return Response.data(goodsSubWarehouseMapper.selectPage(page, queryWrapper));
	}
}
